package com.example.eeuse.model;

import cn.hutool.core.lang.UUID;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Data
public class VedaRequest {

    private Session session;
    private ObFlightSr obFlightSr;
    private String searchDate;
    private String cabinOrder;
    private List<String> airline;
    @Data
    public static class Session{
       private ExtraCooperator extraCooperator;
       private String channel;
       private boolean noRefundReissue;
       private String vipNumber;
       private String childPolicy;
       private String datePolicy;

    }

    @Data
    public static class ObFlightSr{
        private String dep;
        private String arr;
        private String date;
    }
    @Data
    public static class ExtraCooperator{
        private List<Code> code;
    }
    @Data
    public static class Code{
        private List<String> fareBasis;

        private String value;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Code code = (Code) o;
            return Objects.equals(value, code.value);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value);
        }
    }

    public VedaRequestDto toVedaRequestDto()
    {
        if(this.getSession()==null&&this.getSession().getExtraCooperator()==null&&
        this.getSession().getExtraCooperator().getCode()==null)
        {
            return null;
        }
        VedaRequestDto vedaRequestDto=new VedaRequestDto();
        vedaRequestDto.setChannel(this.getSession().getChannel());
        StringBuilder sb=new StringBuilder();
        this.getSession().getExtraCooperator().getCode().forEach(v->{
            sb.append(v.getValue()+",");
        });
        vedaRequestDto.setSession(sb.toString());
        vedaRequestDto.setArr(this.getObFlightSr().getArr());
        vedaRequestDto.setDep(this.getObFlightSr().getDep());
        vedaRequestDto.setDate(this.getObFlightSr().getDate());
        vedaRequestDto.setId(UUID.fastUUID().toString());
        return vedaRequestDto;
    }


    public void addVedaToSet(Map<String,VedaRequestDto> singleMap)
    {
        StringBuilder sb=new StringBuilder();
        if(this.getSession()==null||this.getSession().getExtraCooperator()==null||
                this.getSession().getExtraCooperator().getCode()==null)
        {
            sb.append("NO_SESSION,");
        }
        else {
            this.getSession().getExtraCooperator().getCode().stream().filter(v-> StringUtils.hasText(v.getValue())).distinct().sorted(Comparator.comparing(Code::getValue)).collect(Collectors.toList())
                    .forEach(v-> sb.append(v.getValue()+","));
        }

        String key=this.getSession().getChannel()+"_"+sb;
        singleMap.computeIfAbsent(key,vedaReq-> {
            VedaRequestDto vedaRequestDto=new VedaRequestDto();
            vedaRequestDto.setSession("NO_SET");
            vedaRequestDto.setSessions(sb.toString());
            vedaRequestDto.setArr(this.getObFlightSr().getArr());
            vedaRequestDto.setDep(this.getObFlightSr().getDep());
            vedaRequestDto.setDate(this.getObFlightSr().getDate());
            vedaRequestDto.setId(UUID.fastUUID().toString());
            vedaRequestDto.setSearchDate(this.getSearchDate());
            vedaRequestDto.setChannel(this.getSession().getChannel());
            vedaRequestDto.setReqCount(0);
            return vedaRequestDto;
        }).incReqCount();


    }
}
